package cn.school.demo.ui.edit

import android.os.Bundle
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.ImageView
import androidx.databinding.DataBindingUtil
import androidx.navigation.fragment.findNavController
import cn.school.demo.MyApplication
import cn.school.demo.R
import cn.school.demo.databinding.FragmentUpdatePhoneBinding
import com.blankj.utilcode.util.LogUtils
import com.mylhyl.circledialog.CircleDialog

class UpdatePhoneFragment : Fragment() {

    private lateinit var binding: FragmentUpdatePhoneBinding
    private var isCheck = false

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding =
            DataBindingUtil.inflate(inflater, R.layout.fragment_update_phone, container, false)
        binding.my = MyApplication.Companion
        binding.lifecycleOwner = viewLifecycleOwner
        binding.vw = this
        return binding.root
    }

    fun gotoSetting() = MyApplication.goto(
        R.id.action_UpdatePhone_to_Setting,
        findNavController(),
        parentFragmentManager
    )

    fun gotoModifyPhone() {
        CircleDialog.Builder().setTitle("更改手机号前需验证你的密码")
            .setBodyView(R.layout.circle_body_dialog) {
                val editText = it.findViewById<EditText>(R.id.circle_body_editText)
                val eye = it.findViewById<ImageView>(R.id.circle_body_eye)
                eye.setBackgroundResource(R.drawable.unbrowse)
                eye.setOnClickListener { view ->
                    if (!isCheck) {
                        view.setBackgroundResource(R.drawable.browse)
                        editText.transformationMethod =
                            HideReturnsTransformationMethod.getInstance()
                        isCheck = !isCheck
                    } else {
                        view.setBackgroundResource(R.drawable.unbrowse)
                        editText.transformationMethod = PasswordTransformationMethod.getInstance()
                        isCheck = !isCheck
                    }
                }
            }
            .setPositiveBody("确定") {
                val editText = it.findViewById<EditText>(R.id.circle_body_editText)
                LogUtils.e(editText.text)
                if (MyApplication.user!!.password == editText.text.toString()) {
                    MyApplication.goto(
                        R.id.action_UpdatePhone_to_ModifyPhone,
                        findNavController(),
                        parentFragmentManager
                    )
                } else {
                    CircleDialog.Builder().setText("密码输入错误！")
                        .setNegative("确定", null).show(parentFragmentManager)
                }
                true
            }
            .setNegative("取消", null)
            .show(parentFragmentManager)
    }
}